<!--
Copyright (c) 2022 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL2 draw functions have expected behavior with compositing</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"> </script>
<script src="../../js/tests/compositing-test.js"></script>
<style>
body {
    height: 3000px;
}
</style>
</head>
<body>
<!-- Important to put the canvas at the top so that it's always visible even in the test suite runner.
     Otherwise it just doesn't get composited in Firefox. -->
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";

description(`\
This test ensures WebGL implementations correctly clear the drawing buffer
on composite if preserveDrawingBuffer is false and is not cleared if
preserveDrawingBuffer is true.`);
debug("");

const wtu = WebGLTestUtils;

async function runCompositingTests() {
    const compositingTestFn = createCompositingTestFn({
      webglVersion: 2,
      shadersFn(gl) {
        const vs = `\
        #version 300 es
        in vec4 position;
        void main() {
          gl_Position = position;
        }
        `;
        const fs = `\
        #version 300 es
        precision mediump float;
        out vec4 fragColor;
        void main() {
          fragColor = vec4(1, 0, 0, 1);
        }
        `;
        return [vs, fs];
      },
    });

    function drawArrays(gl) {
      gl.drawArrays(gl.TRIANGLES, 0, 6);
    }

    function drawElements(gl) {
      gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0);
    }

    function drawArraysInstanced(gl) {
      gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, 1);
    }

    function drawElementsInstanced(gl) {
      gl.drawElementsInstanced(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0, 1);
    }

    function drawRangeElements(gl) {
      gl.drawRangeElements(gl.TRIANGLES, 0, 5, 6, gl.UNSIGNED_BYTE, 0);
    }

    await compositingTestFn(drawArrays);
    await compositingTestFn(drawElements);
    await compositingTestFn(drawArraysInstanced);
    await compositingTestFn(drawElementsInstanced);
    await compositingTestFn(drawRangeElements);
    finishTest();
}
runCompositingTests();

var successfullyParsed = true;
</script>
</body>
</html>
